class choujiangUI extends egret.Sprite {

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.createView, this);
    }

    private createView(): void {

        //背景
        var bg = createmap("bg_jpg", 0, 0, true);
        this.addChild(bg);
        var Waterlines_bg1 = createmap("Waterlines_bg_png", 0, 0, true);
        var Waterlines_bg2 = createmap("Waterlines_bg_png", 0, -1130, true);
        this.addChild(Waterlines_bg1);
        this.addChild(Waterlines_bg2);
        egret.Tween.get(Waterlines_bg1, { loop: true }).to({ y: 1136 }, 100000).to({ y: 0 });
        egret.Tween.get(Waterlines_bg2, { loop: true }).to({ y: 0 }, 100000).to({ y: -1130 });



        //显示所得奖品金牌
        var gold = createmap("jinpai_png", 43, 0, true);
        this.addChild(gold);

        //金牌上的奖品图片
        var wenhao = createmap("wenhao_png", 135, 165, true);
        this.addChild(wenhao);

        var pr1 = createmap("prize1_png", 100, 145, false);
        this.addChild(pr1);

        var pr2 = createmap("prize2_png", 100, 145, false);
        this.addChild(pr2);

        var pr3 = createmap("prize3_png", 100, 145, false);
        this.addChild(pr3);

        var pr4 = TextFiled("谢谢参与", 125, 185, 40, 0x999999, "left", 100);
        pr4.visible = false;
        this.addChild(pr4);

        var pr5 = createmap("prize5_png", 100, 145, false);
        this.addChild(pr5);

        var pr6 = createmap("prize6_png", 100, 145, false);
        this.addChild(pr6);

        var pr7 = createmap("prize7_png", 100, 145, false);
        this.addChild(pr7);

        var pr8 = createmap("prize8_png", 100, 145, false);
        this.addChild(pr8);


        //右上显示奖品名称
        var PrizeName = TextFiled("？ ？ ？ ？ ？", 326, 140, 43, 0xffffff, "left", 265, 160, "middle");
        this.addChild(PrizeName);

        var wish = TextFiled("恭喜你！", 330, 140, 42, 0xffffff, "left");
        wish.visible = false;
        this.addChild(wish);

        var Yourprize = TextFiled("你的奖品为", 330, 197, 42, 0xffffff, "left");
        Yourprize.visible = false;
        this.addChild(Yourprize);

        var wen1 = TextFiled("奥利帝克户外包", 330, 250, 42, 0xffffff, "left", 270, 100);
        wen1.visible = false;
        this.addChild(wen1);

        var wen2 = TextFiled("迪卡侬透气T恤", 330, 250, 42, 0xffffff, "left", 270, 100);
        wen2.visible = false;
        this.addChild(wen2);

        var wen3 = TextFiled("乐扣运动水壶", 330, 250, 42, 0xffffff, "left", 270, 100);
        wen3.visible = false;
        this.addChild(wen3);

        var wen4 = TextFiled("谢谢参与", 330, 250, 42, 0xffffff, "left", 270, 100, "middle");
        wen4.visible = false;
        this.addChild(wen4);

        var wen5 = TextFiled("迪卡侬运动毛巾", 330, 250, 42, 0xffffff, "left", 270, 100);
        wen5.visible = false;
        this.addChild(wen5);

        var wen6 = TextFiled("多功能运动腰包", 330, 250, 42, 0xffffff, "left", 270, 100);
        wen6.visible = false;
        this.addChild(wen6);

        var wen7 = TextFiled("安联运动意外保障计划（7天）", 330, 250, 38, 0xffffff, "left", 270, 100);
        wen7.visible = false;
        this.addChild(wen7);

        var wen8 = TextFiled("小米运动手环", 330, 250, 42, 0xffffff, "left", 270, 100);
        wen8.visible = false;
        this.addChild(wen8);


        //抽奖九宫格容器
        var Luckdraw = new egret.DisplayObjectContainer();
        this.addChild(Luckdraw);
        Luckdraw.width = 560;
        Luckdraw.height = 560;
        Luckdraw.x = 45;
        Luckdraw.y = 396;


        //放奖品进格九宫格

        var prize1 = rongqidiv(0xfffefd, 10, 0xdbdcdc, 1, false, 0, 0, 170, 0, 170, 180, 0, 180, 0, 0, 0, 0);
        Luckdraw.addChild(prize1);
        //奖品下面的黄色背景
        var jp_bg1 = createmap("jp_bg_png", -14, -10, false, "", 0, 0, 0.9, 0.9);
        prize1.addChild(jp_bg1);
        //奖品
        var beibao = createmap("prize1_png", 20, -24, true);
        prize1.addChild(beibao)
        var baoWen = TextFiled("奥利帝克户外包", 18, 139, 20, 0x000000, "left", 145);
        prize1.addChild(baoWen);


        var prize2 = rongqidiv(0xfffefd, 10, 0xdbdcdc, 1, true, 0, 0, 170, 0, 170, 180, 0, 180, 0, 0, 190, 0);
        Luckdraw.addChild(prize2);
        //奖品下面的黄色背景
        var jp_bg2 = createmap("jp_bg_png", -14, -10, false, "", 0, 0, 0.9, 0.9);
        prize2.addChild(jp_bg2);
        //奖品
        var Txue = createmap("prize2_png", 14, -24, true);
        prize2.addChild(Txue);
        var TxueWen = TextFiled("迪卡侬透气T恤", 24, 139, 20, 0x000000, "left", 145);
        prize2.addChild(TxueWen);


        var prize3 = rongqidiv(0xfffefd, 10, 0xdbdcdc, 1, true, 0, 0, 170, 0, 170, 180, 0, 180, 0, 0, 380, 0);
        Luckdraw.addChild(prize3);
        //奖品下面的黄色背景
        var jp_bg3 = createmap("jp_bg_png", -14, -10, false, "", 0, 0, 0.9, 0.9);
        prize3.addChild(jp_bg3);
        //奖品
        var shuihu = createmap("prize3_png", 12, -24, true);
        prize3.addChild(shuihu);
        var shuihuWen = TextFiled("乐扣运动水壶", 32, 139, 20, 0x000000, "left", 145);
        prize3.addChild(shuihuWen);


        var prize4 = rongqidiv(0xfffefd, 10, 0xdbdcdc, 1, true, 0, 0, 170, 0, 170, 180, 0, 180, 0, 0, 380, 200);
        Luckdraw.addChild(prize4);
        //奖品下面的黄色背景
        var jp_bg4 = createmap("jp_bg_png", -14, -10, false, "", 0, 0, 0.9, 0.9);
        prize4.addChild(jp_bg4);
        //奖品
        var Thanks = TextFiled("谢谢参与", 48, 48, 40, 0x999999, "", 80);
        Thanks.lineSpacing = 10;
        prize4.addChild(Thanks);


        var prize5 = rongqidiv(0xfffefd, 10, 0xdbdcdc, 1, true, 0, 0, 170, 0, 170, 180, 0, 180, 0, 0, 380, 400);
        Luckdraw.addChild(prize5);
        //奖品下面的黄色背景
        var jp_bg5 = createmap("jp_bg_png", -14, -10, false, "", 0, 0, 0.9, 0.9);
        prize5.addChild(jp_bg5);
        //奖品
        var maojin = createmap("prize5_png", 15, 0, true);
        prize5.addChild(maojin);
        var maojinWen = TextFiled("迪卡侬运动毛巾", 20, 135, 20, 0x000000, "left", 145);
        prize5.addChild(maojinWen);


        var prize6 = rongqidiv(0xfffefd, 10, 0xdbdcdc, 1, true, 0, 0, 170, 0, 170, 180, 0, 180, 0, 0, 190, 400);
        Luckdraw.addChild(prize6);
        //奖品下面的黄色背景
        var jp_bg6 = createmap("jp_bg_png", -14, -10, false, "", 0, 0, 0.9, 0.9);
        prize6.addChild(jp_bg6);
        //奖品
        var yaobao = createmap("prize6_png", 12, 15, true);
        prize6.addChild(yaobao);
        var yaobaoWen = TextFiled("多功能运动腰包", 22, 140, 20, 0x000000, "left", 145);
        prize6.addChild(yaobaoWen);


        var prize7 = rongqidiv(0xfffefd, 10, 0xdbdcdc, 1, true, 0, 0, 170, 0, 170, 180, 0, 180, 0, 0, 0, 400);
        Luckdraw.addChild(prize7);
        //奖品下面的黄色背景
        var jp_bg7 = createmap("jp_bg_png", -14, -10, false, "", 0, 0, 0.9, 0.9);
        prize7.addChild(jp_bg7);
        //奖品
        var baozheng = createmap("prize7_png", 20, 0, true);
        prize7.addChild(baozheng);
        var baozhengWen = TextFiled("安联运动意外保障计划（7天）", 20, 115, 20, 0x000000, "left", 145);
        prize7.addChild(baozhengWen);


        var prize8 = rongqidiv(0xfffefd, 10, 0xdbdcdc, 1, true, 0, 0, 170, 0, 170, 180, 0, 180, 0, 0, 0, 200);
        Luckdraw.addChild(prize8);
        //奖品下面的黄色背景
        var jp_bg8 = createmap("jp_bg_png", -14, -10, false, "", 0, 0, 0.9, 0.9);
        prize8.addChild(jp_bg8);
        //奖品
        var shouhuan = createmap("prize8_png", 12, 10, true);
        prize8.addChild(shouhuan);
        var shouhuanWen = TextFiled("小米运动手环", 33, 135, 20, 0x000000, "left", 145)
        prize8.addChild(shouhuanWen);

        //抽奖按钮
        var start_btn = createmap("Luckdraw_btn1_png", 185, 196, true, "", 0, 0, 1, 1.05);
        Luckdraw.addChild(start_btn);
        start_btn.touchEnabled = true;
        var btn_wen = TextFiled("点击抽奖", 220, 220, 58, 0xff4c22, "left", 150);
        Luckdraw.addChild(btn_wen);


        //底部logo
        var Luckdrawlogo = createmap("Luckdraw_logo_png", 227, 1000, true);
        Luckdrawlogo.touchEnabled = true;
        this.addChild(Luckdrawlogo);



        //抽奖后的遮罩
        var mask = rongqi(0x042730, 0.8, 0, 0, 640, 0, 640, 1136, 0, 1136, 0, 0, 0, 0);
        mask.scaleY = 0;
        mask.anchorOffsetX = mask.width * .5;
        mask.anchorOffsetY = mask.height * .5;
        mask.touchEnabled = true;
        this.addChild(mask);
        var fenxiang = createmap("Luckdraw_share_png", 170, 40, true);
        mask.addChild(fenxiang);
        var wenan = TextFiled("我刚参加奥运指尖自由游比赛，你也来参加吧！", 175, 522, 45, 0xffffff, "left", 335);
        wenan.lineSpacing = 10;
        mask.addChild(wenan);

        //点击遮罩关闭遮罩
        mask.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            egret.Tween.get(mask).to({ scaleY: 0 }, 300);
        }, this)

        //加载抽奖音效
        var sound = new egret.Sound();
        sound.load("resource/assets/8400.wav");

        var prizeSound = new egret.Sound();
        prizeSound.load("resource/assets/5986.wav")


        //抽奖次数
        var cishu = 1;
        var onoff = true;//防止九宫格没停止就点再次抽奖
        //抽奖
        var target = [41, 42, 43, 44, 45, 46, 47, 48];//跑的次数
        var award //奖品
        //开始抽奖按钮
        start_btn.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            if (onoff) {
                onoff = false;

                award = 0;
                if (cishu >= 1) {
                    btn_wen.text = "抽奖中...";
                    dingshiqi();
                }
                cishu--;
            }
        }, this);
        //封装
        function dingshiqi() {
            var awardArr = [jp_bg1, jp_bg2, jp_bg3, jp_bg4, jp_bg5, jp_bg6, jp_bg7, jp_bg8];//奖品背景数组
            var gold_prize = [pr1, pr2, pr3, pr4, pr5, pr6, pr7, pr8];//金牌上的奖品
            var wen_Arr = [wen1, wen2, wen3, wen4, wen5, wen6, wen7, wen8];//右上角奖品名称
            for (var i = 0; i < awardArr.length; i++) {
                awardArr[i].visible = false;
                gold_prize[i].visible = false;
                wen_Arr[i].visible = false;
            }
            var num = 1;//黄色背景跳动次数，由上个奖品跳到下个奖品
            var speed = 400;//调用定时器时间
            var time = setTimeout(timer, speed);//定时器
            PrizeName.visible = false;//点抽奖前右上角的问号
            wenhao.visible = false;//抽奖前金牌上的问号
            wish.visible = true;//点抽奖后右上角的恭喜你
            Yourprize.visible = true;//点抽奖后右上角的“你的奖品为”

            //定时器调用函数
            function timer() {
                // egret.Tween.get(awardArr[award]).to({ visible: true }).to({ visible: false }, 200);
                // egret.Tween.get(gold_prize[award]).to({ visible: true }).to({ visible: false }, 200);
                // egret.Tween.get(wen_Arr[award]).to({ visible: true }).to({ visible: false }, 200);
                award++;//奖品下标
                sound.play(0, 1);//抽奖音效
                if (award == 8) {
                    award = 0;
                }
                num++;
                if (num < 3) {
                    speed += 10;
                }
                if (num > 3 && num < 20) {
                    speed -= 35;
                    if (speed < 150) {
                        speed = 150;
                    }
                }
                if (num > 30) {
                    speed += 30;
                };
                for (var i = 0; i < awardArr.length; i++) {
                    awardArr[i].visible = false;    //将第一个隐藏
                    gold_prize[i].visible = false;
                    wen_Arr[i].visible = false;
                }
                awardArr[award].visible = true;     //将第二个显示
                gold_prize[award].visible = true;
                wen_Arr[award].visible = true;

                if (num == target[3]) {//控制奖品 0-7
                    setTimeout(function () {
                        // awardArr[award].visible = true;
                        // gold_prize[award].visible = true;
                        // wen_Arr[award].visible = true;
                        onoff = true;
                        btn_wen.text = "再抽一次";
                        prizeSound.play(0, 1);//九宫格停止后的音效
                        setTimeout(function () {//抽奖后的遮罩出现
                            egret.Tween.get(mask).to({ scaleY: 1 }, 300);
                        }, speed + 800);
                    }, speed);
                    return;
                };
                time = setTimeout(timer, speed);
            }
        }
    }
}
